﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using System.Data.SqlClient;
using System.Reflection;
//这是F


namespace 共享单车管理系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int flag = 1;
        public static bool Aflag = false;
        public static bool Bflag = false;
        public static string BikeID = "";


        void showALLProduct()
        {
            DB.GetCn();

            string str = "select *from Bike";

            SqlCommand cmd = new SqlCommand(str, DB.cn);

            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
            
                int index = dgvBikeType.Rows.Add();

                dgvBikeType.Rows[index].Cells[0].Value = rdr[0];
                dgvBikeType.Rows[index].Cells[1].Value = rdr[1];
                dgvBikeType.Rows[index].Cells[2].Value = rdr[2];
                dgvBikeType.Rows[index].Cells[3].Value = rdr[3];
                dgvBikeType.Rows[index].Cells[4].Value = rdr[4];
                dgvBikeType.Rows[index].Cells[5].Value = rdr[6];
                dgvBikeType.Rows[index].Cells[6].Value = rdr[7];

                try
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + rdr[5]);
                    dgvBikeType.Rows[index].Cells["Column8"].Value = imageColum;
                }
                catch
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + "//" + "无.gif");
                    dgvBikeType.Rows[index].Cells["Column8"].Value = imageColum;
                }
            }
            rdr.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            label7.Parent = label6;
            dgvBikeType.RowTemplate.Height = 90;
            showALLProduct();
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            dgvBikeType.Rows.Clear();

            showALLProduct();
        }

        private void btnSearch1_Click(object sender, EventArgs e)
        {

            if (txtname.Text == "")
            {
                MessageBox.Show("请输入查询关键字");
            }
            else
            {

                dgvBikeType.Rows.Clear();

                DB.GetCn();

                string str = "select * from Bike where BikeID like'%" + txtname.Text + "%'";

                SqlCommand cmd = new SqlCommand(str, DB.cn);

                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {

                    int index = dgvBikeType.Rows.Add();

                    dgvBikeType.Rows[index].Cells[0].Value = rdr[0];
                    dgvBikeType.Rows[index].Cells[1].Value = rdr[1];
                    dgvBikeType.Rows[index].Cells[2].Value = rdr[2];
                    dgvBikeType.Rows[index].Cells[3].Value = rdr[3];
                    dgvBikeType.Rows[index].Cells[4].Value = rdr[4];
                    dgvBikeType.Rows[index].Cells[5].Value = rdr[6];
                    dgvBikeType.Rows[index].Cells[6].Value = rdr[7];
                    try
                    {
                        Image imageColum = Image.FromFile(Application.StartupPath + rdr[5]);
                        dgvBikeType.Rows[index].Cells["Column8"].Value = imageColum;
                    }
                    catch
                    {
                        Image imageColum = Image.FromFile(Application.StartupPath + "//" + "无.gif");
                        dgvBikeType.Rows[index].Cells["Column8"].Value = imageColum;
                    }
                }
                rdr.Close();

            }

        }

        private void dgvBikeType_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            if(CIndex==8)
            {
                int rowindex = e.RowIndex;
                BikeID = dgvBikeType.Rows[rowindex].Cells[0].Value.ToString();
                Tproducts t1=new Tproducts();
                t1.ShowDialog();
            }
            if(CIndex==9)
            {
                if(Aflag==false)
                {
                    MessageBox.Show("请先登录");
                    Login ll = new Login();
                    ll.ShowDialog();
                }
                else
                {
                    int rowidex = e.RowIndex;
                    BikeID = dgvBikeType.Rows[rowidex].Cells[0].Value.ToString();
                    if (Convert.ToInt32(dgvBikeType.Rows[rowidex].Cells[6].Value) < 1 || dgvBikeType.Rows[rowidex].Cells[2].Value.ToString() == "良好")
                    {
                        MessageBox.Show("该单车未有闲置或已损坏");
                    }
                    else
                    {
                        shops s1 = new shops();
                        s1.ShowDialog();
                    }
                 
                }
            }
            if (CIndex == 10)
            {
                if (Aflag == false)
                {
                    MessageBox.Show("请先登录");
                    Login ll = new Login();
                    ll.ShowDialog();
                }
                guihuandanche w1= new guihuandanche();
                w1.ShowDialog();
            }


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnSearch2_Click(object sender, EventArgs e)//价格f
        {
            
            dgvBikeType.Rows.Clear();
            string str = "";
            DB.GetCn();
            int i = comboBox1.SelectedIndex;
            switch(i)
            {
                case 0:
                    str = "单人脚踏";
                    break;
                case 1:
                    str = "双人脚踏";
                    break;
                case 2:
                    str = "三人脚踏";
                    break;
                case 3:
                    str = "单人电动";
                    break;
                default:
                    str = "双人电动";
                    break;
            }
            string sdr = "select * from Bike where BikeType = '" + str + "'";

            SqlCommand cmd = new SqlCommand(sdr, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {

                int index = dgvBikeType.Rows.Add();

                dgvBikeType.Rows[index].Cells[0].Value = rdr[0];
                dgvBikeType.Rows[index].Cells[1].Value = rdr[1];
                dgvBikeType.Rows[index].Cells[2].Value = rdr[2];
                dgvBikeType.Rows[index].Cells[3].Value = rdr[3];
                dgvBikeType.Rows[index].Cells[4].Value = rdr[4];
                dgvBikeType.Rows[index].Cells[5].Value = rdr[6];
                dgvBikeType.Rows[index].Cells[6].Value = rdr[7];
                try
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + rdr[5]);
                    dgvBikeType.Rows[index].Cells["Column8"].Value = imageColum;
                }
                catch
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + "//" + "无.gif");
                    dgvBikeType.Rows[index].Cells["Column8"].Value = imageColum;
                }
            }
            rdr.Close();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            this.Hide();
            Login ll = new Login();
            ll.ShowDialog();
            if(flag==1)
            {
                this.Visible = true;
                label5.Text = Login.UserID;
            }


        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void btnreginster_Click(object sender, EventArgs e)
        {
            Register rl = new Register();
            rl.ShowDialog();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label7.Top = label6.Height / 2;
            if(label7.Left < -label7.Width)
            {
                label7.Left = label6.Width ;
            }
            label7.Left -= 5;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
               
                guzhuang h1 = new guzhuang();
                h1.ShowDialog();

            

        }

        private void button2_Click(object sender, EventArgs e)
        {
           map m1 = new map();
            m1.ShowDialog();
            
        }

      
    }
}
